package com.vastcom.sigma.admin.consume;

import com.vastcom.sigma.admin.model.Materiainventory;
import com.vastcom.sigma.admin.model.Materiainventorychange;
import com.vastcom.sigma.admin.model.Rawbeaninventory;
import com.vastcom.sigma.admin.model.Rawbeaninventorychange;
import com.vastcom.sigma.admin.service.ManageService;
import com.vastcom.sigma.admin.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

@Component
@EnableJms
public class JmsConsume {

    @Autowired
    private ManageService manageService;


    @JmsListener(destination = "rawBeanInventoryUpdate")
    public void rawBeanInventoryUpdate(Rawbeaninventorychange parm) throws Exception{


        Rawbeaninventory inventory=manageService.getRawbeaninventoryByPmId(parm.getBusid());
        if(inventory!=null){
            double total=inventory.getTotal();
            if(parm.getType().equals(Tools.INVENTORY_CHANGE_TYPE_ADD)){
                total=total+parm.getChangetotal();
            }
            else if(parm.getType().equals(Tools.INVENTORY_CHANGE_TYPE_MENUS)) {
                total=total-parm.getChangetotal();
            }
            inventory.setTotal(total);
            manageService.saveRawbeaninventory(inventory);
        }
        else {
            Rawbeaninventory temp=new Rawbeaninventory();
            temp.setPm(parm.getBusid());
            temp.setTotal(parm.getChangetotal());
            manageService.saveRawbeaninventory(temp);
        }

        parm.setChangedate(new Timestamp(System.currentTimeMillis()));
        manageService.saveRawbeaninventorychange(parm);

    }

    //

    @JmsListener(destination = "materiaInventoryUpdate")
    public void materiaInventoryUpdate(Materiainventorychange parm) throws Exception{

        Materiainventory inventory=manageService.getMateriainventoryByPm(parm.getBusid());
        if(inventory!=null){
            int total=inventory.getTotal();
            if(parm.getType().equals(Tools.INVENTORY_CHANGE_TYPE_ADD)){
                total=total+parm.getChangetotal();
            }
            else if(parm.getType().equals(Tools.INVENTORY_CHANGE_TYPE_MENUS)){
                total=total-parm.getChangetotal();
            }
            inventory.setTotal(total);
            manageService.saveMateriainventory(inventory);


        }
        else {
            Materiainventory temp=new Materiainventory();
            temp.setPm(parm.getBusid());
            temp.setTotal(parm.getChangetotal());
            manageService.saveMateriainventory(temp);
        }


        parm.setChangedate(new Timestamp(System.currentTimeMillis()));
       manageService.saveMateriainventorychange(parm);

    }

}
